<?php

namespace Admin\Controller;

class CategoryController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Category");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取分类列表
        $list = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    /**
     * 添加数据
     */
    public function addPost() {

        $categoryName = $_REQUEST["categoryName"];

        $categoryMap["category_name"] = $categoryName;

        $category = $this->currentModel->queryByMap($categoryMap);

        if(!isNullOrEmpty($category)) {
            $this->error("对不起,名称重复,请重新填写");
        }

        $categoryData["category_name"] = $categoryName;
        $categoryData["create_time"] = getCurrentDate();
        $categoryData["is_online"] = C("DEFAULT_YN.YES");

        $this->currentModel->add($categoryData);

        $this->success("添加成功", U("category/index"));

    }

    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];

        $category = $this->currentModel->queryById($id);

        $categoryName = $_REQUEST["categoryName"];

        $categoryMap["category_name"] = $categoryName;
        $categoryMap["category_id"] = array("neq", $category["category_id"]);

        $repeatCategory = $this->currentModel->queryByMap($categoryMap);

        if(!isNullOrEmpty($repeatCategory)) {
            $this->error("对不起,名称已经存在,请重新填写");
        }

        $category["category_name"] = $categoryName;

        $this->currentModel->updateInfo($category);

        $this->success("更新成功", U("category/index"));

    }

    /**
     * 上架
     */
    public function online() {

        $id = $_REQUEST["id"];

        $category = $this->currentModel->queryById($id);

        $category["is_online"] = C("DEFAULT_YN.YES");

        $this->currentModel->updateInfo($category);

        $this->success("上架成功", U("category/index"));

    }

    /**
     * 下架
     */
    public function offline() {

        $id = $_REQUEST["id"];

        $category = $this->currentModel->queryById($id);

        $category["is_online"] = C("DEFAULT_YN.NO");

        $this->currentModel->updateInfo($category);

        $this->success("下架成功", U("category/index"));

    }

}